package com.sndscl.rpc.spring;

import com.sndscl.rpc.common.annotation.SndsclRpcInterface;
import com.sndscl.rpc.common.annotation.SndsclRpcService;
import com.sndscl.rpc.common.util.ClazzInvockUtil;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @Description
 * @Author lsh
 * @Date 2020/10/14 20:14
 **/
@Component
public class SndSclBeanPostProcessor implements BeanPostProcessor {

    private static Set<String> interfaceList = new HashSet<>();
    private static Map<String,String> beanMap = new HashMap<>();

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        SndsclRpcService sndsclRpcService = bean.getClass().getDeclaredAnnotation(SndsclRpcService.class);
        if(sndsclRpcService != null){
            Class<?>[] interfaces = bean.getClass().getInterfaces();
            for(Class<?> clazz : interfaces){
                SndsclRpcInterface sndsclRpcInterface = clazz.getDeclaredAnnotation(SndsclRpcInterface.class);
                if(sndsclRpcInterface != null){
                    Set<String> rpcInterface=  ClazzInvockUtil.getInterfaceMethodSign(clazz);
                    interfaceList.addAll(rpcInterface);
                    beanMap.put(clazz.getName(),beanName);
                }
            }
        }
        return bean;
    }

    public static Set<String> getInterfaceList() {
        return interfaceList;
    }

    public static String getBeanName(String className){
        return beanMap.get(className);
    }
}
